package com.yjb.demo.tdengine.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.yjb.demo.tdengine.constant.TDEngineConstant;
import com.yjb.demo.tdengine.domain.dto.IotDevicePointDataDTO;
import com.yjb.demo.tdengine.domain.dto.IotDeviceStateDTO;
import com.yjb.demo.tdengine.domain.dto.TdInsertDTO;
import com.yjb.demo.tdengine.mapper.TaosMapper;
import com.yjb.demo.tdengine.service.TaosService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author yinjinbiao
 * @create 2024/6/13 14:41
 */
@Service
public class TaosServiceImpl implements TaosService {

    @Resource
    private TaosMapper taosMapper;


    @Override
    public void createTable(String stableName, Long deviceId, String location) {
        taosMapper.createStable(stableName);
        taosMapper.createTable(stableName,deviceId,location);
    }

    @Override
    public void addColumn(String stableName, String columnName, String columnType) {
        taosMapper.addColumn(stableName,columnName,columnType);
    }

    @Override
    public void insert(TdInsertDTO tdInsertDTO) {
        taosMapper.insertData(tdInsertDTO);
    }

    @Override
    public IotDeviceStateDTO getLastRow(Long deviceId) {
        IotDeviceStateDTO iotDeviceStateDTO = new IotDeviceStateDTO();
        iotDeviceStateDTO.setId(deviceId);
        HashMap<String, Object> lastRow = taosMapper.getLastRow(deviceId);
        if (lastRow == null){
            return null;
        }
        Map<String, Object> res = new HashMap();
        // 解析出原有列名返回
        String prefix = "last_row(";
        String suffix = ")";
        lastRow.keySet().forEach(key -> {
            // 计算前后缀在字符串中的位置
            int startIndex = key.indexOf(prefix) + prefix.length(); // 获取前缀后面第一个字符的索引
            int endIndex = key.indexOf(suffix); // 获取后缀第一个字符的索引
            String columnName = key.substring(startIndex, endIndex);
            if (TDEngineConstant.COLUMN_TS.equals(columnName)){
                iotDeviceStateDTO.setTs((Date) lastRow.get(key));
            }else {
                res.put(columnName, lastRow.get(key));
            }
        });
        iotDeviceStateDTO.setProperties(res);
        return iotDeviceStateDTO;
    }

    @Override
    public List<IotDevicePointDataDTO> getPointData(Long deviceId, String columnName, Date startTime, Date endTime) {
        List<HashMap<String, Object>> list = taosMapper.getPointData(deviceId, columnName, startTime, endTime);
        if (CollUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        List<IotDevicePointDataDTO> iotDevicePointDataDTOList = new ArrayList<>();
        list.forEach(map->{
            IotDevicePointDataDTO iotDevicePointDataDTO = new IotDevicePointDataDTO();
            iotDevicePointDataDTO.setTs((Date) map.get(TDEngineConstant.COLUMN_TS));
            iotDevicePointDataDTO.setValue(map.get(columnName));
            iotDevicePointDataDTOList.add(iotDevicePointDataDTO);
        });
        return iotDevicePointDataDTOList;
    }


}
